package com.sort;

import java.util.Arrays;

/**
 * 冒泡排序
 *
 * @author zhongyi_yang
 */
public class BubbleSort {
	/**
	 * 冒泡排序
	 *
	 * @param dataWraps
	 */
	public static void bubbleSort(DataWrap[] dataWraps) {
		System.out.println("开始排序：");
		int arrayLength = dataWraps.length;
		for (int i = 0; i < arrayLength - 1; i++) {
			boolean flag = false;
			for (int j = 0; j < arrayLength - 1 - i; j++) {
				// 如果j索引处的元素大于j+1处的元素
				if (dataWraps[j].compareTo(dataWraps[j + 1]) > 0) {
					// 交换它们
					DataWrap tmpDataWrap = dataWraps[j + 1];
					dataWraps[j + 1] = dataWraps[j];
					dataWraps[j] = tmpDataWrap;
					flag = true;
				}
			}
			System.out.println(Arrays.toString(dataWraps));
			// 如果某趟没有发生交换，则表明已处于有序状态
			if (!flag) {
				break;
			}
		}
	}

	public static void main(String[] args) {
		DataWrap dataWrap[] = {new DataWrap(9, ""), new DataWrap(16, ""),
				new DataWrap(21, "*"), new DataWrap(23, ""),
				new DataWrap(30, ""), new DataWrap(49, ""),
				new DataWrap(21, ""), new DataWrap(30, "*")};
		System.out.println("排序前：" + Arrays.toString(dataWrap));
		bubbleSort(dataWrap);
		System.out.println("排序后：" + Arrays.toString(dataWrap));
	}
}
